//
//  BrandCarView.m
//  AutoHome
//
//  Created by wqh on 15-5-11.
//  Copyright (c) 2015年 Wqh. All rights reserved.
//

#import "BrandCarView.h"

#import "SecondBrandCarCell.h"
#import "BrandCarModel.h"
#import "CarPicsViewController.h"

#define kSecondBrandCarCellId @"SecondBrandCarCellId"

@interface BrandCarView()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic) NSMutableArray *titleArr;
@property (nonatomic) NSMutableArray *carListArr;

@end

@implementation BrandCarView

- (void)awakeFromNib {
    [self addObserver];
    self.titleArr = [NSMutableArray array];
    self.carListArr = [NSMutableArray array];
    [self createTableView];
    self.segmentedControl.selectedSegmentIndex = 0;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//注册观察者
- (void)addObserver {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadData) name:@"BrandCarDisplay" object:nil];
    
}

- (void)createTableView {
  
    [self.tableView registerNib:[UINib nibWithNibName:@"SecondBrandCarCell" bundle:nil] forCellReuseIdentifier:kSecondBrandCarCellId];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 80;
    
}

#pragma mark loadData
- (void)downloadData {
    
    if (self.titleArr.count != 0) {
        [self.titleArr removeAllObjects];
        [self.carListArr removeAllObjects];
    }
    [[DataEngine sharedInstance] requestBrandCarWithBrandId:self.brandModel.id typeId:1  success:^(id respondsData) {
        [self downloadDataSuccess:respondsData];
    } failed:^(NSError *error) {
        
    }];
}

- (void)downloadDataType2 {
    if (self.titleArr.count != 0) {
        [self.titleArr removeAllObjects];
        [self.carListArr removeAllObjects];
    }
    [[DataEngine sharedInstance] requestBrandCarWithBrandId:self.brandModel.id typeId:2  success:^(id respondsData) {
        [self downloadDataSuccess:respondsData];
    } failed:^(NSError *error) {
        
    }];
}

- (void)downloadDataSuccess:(id)respondsData {
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:respondsData options:NSJSONReadingMutableContainers error:nil];
    
    NSArray *brandArr = dict[@"result"][@"fctlist"];
    int i = 0;
    for (NSDictionary *carListDict in brandArr) {
      
        [self.titleArr addObject:carListDict[@"name"]];
        NSMutableArray *array = [NSMutableArray array];
        for (NSDictionary *carDict in carListDict[@"serieslist"]) {
            BrandCarModel *model = [[BrandCarModel alloc] init];
            [model setValuesForKeysWithDictionary:carDict];
            [array addObject:model];
        }
        [self.carListArr addObject:array];
        i++;
    }

    [self.tableView reloadData];
    
}

#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.titleArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.carListArr[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SecondBrandCarCell *cell = [tableView dequeueReusableCellWithIdentifier:kSecondBrandCarCellId forIndexPath:indexPath];
    BrandCarModel *model = self.carListArr[indexPath.section][indexPath.row];
    [cell showDataWithModel:model];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.titleArr[section];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BrandCarModel *model = self.carListArr[indexPath.section][indexPath.row];
    
    CarPicsViewController *vc = [CarPicsViewController new];
    vc.pic_id = [ model.id stringValue];
    if (self.myPicJumpBlock) {
        self.myPicJumpBlock(vc);
    }
    if (self.myHideTabBar) {
        self.myHideTabBar();
    }
    
}

#pragma mark - 点击分段控制器切换视图
- (IBAction)segmentedClick:(UISegmentedControl *)sender {
    if (sender.selectedSegmentIndex == 0) {
        [self downloadData];
    }
    if (sender.selectedSegmentIndex == 1) {
        [self downloadDataType2];
    }
}



@end
